#include"m_Mysql.h"

int m_MysqlInit(MYSQL **m_mysql,char *host,char *user,char * password,char *database){
    *m_mysql=mysql_init(NULL);
    MYSQL * ret = mysql_real_connect(*m_mysql,host,user,password,database,0,NULL,0);
    printf("MYSQL连接成功!! host=%s user=%s password=%s database=%s\n",host,user,password,database);
    if (ret == NULL)
    {
        fprintf(stderr,"error:%s\n",mysql_error(*m_mysql));
        return -1;
    }
    return 0;
}

int m_MysqlQuery(MYSQL *m_mysql,char * sql){
    int qret = mysql_query(m_mysql,sql);
    if (qret != 0)
    {
        fprintf(stderr,"error:%s\n",mysql_error(m_mysql));
        return -1;
    }
    //printf("qret:%d\n",qret);
    return 0;
}

int m_MysqlRead(MYSQL *m_mysql,MYSQL_ROW *row){
    MYSQL_RES * results = mysql_store_result(m_mysql);
    printf("total rows:%llu\n",mysql_num_rows(results));
    while ((*row = mysql_fetch_row(results)) != NULL)
    {
        for (int i = 0; i < mysql_num_fields(results); i++)
        {
            printf("%s\t",(*row)[i]);
        }
        printf("\n");
    }
    mysql_free_result(results);
}
